package com.javaweb.blogcite.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaweb.blogcite.common.PageData;
import com.javaweb.blogcite.controller.vo.CommentView;
import com.javaweb.blogcite.mapper.BlogMapper;
import com.javaweb.blogcite.mapper.CommentMapper;
import com.javaweb.blogcite.pojo.Blog;
import com.javaweb.blogcite.pojo.Comment;
import com.javaweb.blogcite.service.CommentService;
import com.javaweb.blogcite.util.AvatarUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentMapper commentMapper;

    @Resource
    private BlogMapper blogMapper;

    @Override
    public CommentView setComment(Comment comment) {
        // 添加头像和创建时间
        comment.setAvatar(AvatarUtil.getAvatar());
        comment.setCreateDate(new Date().getTime());
        int rows = commentMapper.insert(comment);

        // blog 表中的 comment_number 加一
        Blog blog = blogMapper.selectById(comment.getBlogId());
        blog.setCommentNumber(blog.getCommentNumber() + 1);
        blogMapper.updateById(blog);

        // 从数据库中获取 blog 和 comment，封装到 CommentView 中
        CommentView commentView = new CommentView(comment, blog);
        return commentView;
    }

    @Override
    public PageData<CommentView> getCommentByPage(Integer page, Integer limit, Integer blogid) {
        if (blogid == -1) {
            Page<Comment> pagehelper = new Page<>(page, limit);
            IPage<Comment> commentIPage = commentMapper.selectPage(pagehelper, null);
            List<Comment> comments = pagehelper.getRecords();
            List<Blog> blogs = blogMapper.selectList(null);
            List<CommentView> commentViews = new ArrayList<>();
            for (Comment comment : comments) {
                Integer id = comment.getBlogId();
                for (Blog blog : blogs) {
                    if (blog.getId() == id) {
                        commentViews.add(new CommentView(comment, blog));
                        break;
                    }
                }
            }
            return new PageData<>(commentIPage.getTotal(), commentViews);
        }
        Blog blog = blogMapper.selectById(blogid);
        IPage<Comment> p = new Page<>(page, limit);
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("blog_id", blogid);
        IPage<Comment> commentIPage = commentMapper.selectPage(p, queryWrapper);
        List<Comment> comments = commentIPage.getRecords();

        List<CommentView> commentViews = new ArrayList<>();
        for (Comment comment : comments) {
            CommentView commentView = new CommentView(comment, blog);
            commentViews.add(commentView);
        }
        return new PageData<>(commentIPage.getTotal(), commentViews);
    }

    @Override
    public boolean deleteComment(Integer id) {
        // 找到所属 blog，将 comment_number 减一
        Comment comment = commentMapper.selectById(id);
        Blog blog = blogMapper.selectById(comment.getBlogId());
        blog.setCommentNumber(blog.getCommentNumber() - 1);
        blogMapper.updateById(blog);

        int rows = commentMapper.deleteById(id);
        return rows > 0;
    }
}
